﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace HobSoft.SoccerBet2014.Domain.Models
{
    using Core;
    using KeyHelper;
    [Serializable]
    [DataContract]
    public class BFMarket : IModel
    {
        public BFMarket()
        {
            Runners = new ObservableCollection<BFRunner>();
        }

        [PrimaryKey]
        [DataMember(IsRequired = true)]
        public string Key
        {
            get { return KeyGenerateHelper.Create<BFMarket>(this); }
            set {  }
        }

        private int _eventID;
        [Key]
        [Required]
        [DataMember(IsRequired = true)]
        public int EventID
        {
            get { return _eventID; }
            set { _eventID = value; }
        }

        private string _marketID;
        [Key]
        [Required]
        [MaxLength(50)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string MarketID
        {
            get { return _marketID; }
            set { _marketID = value; }
        }

        private string _marketName;
        [Required]
        [MaxLength(50)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string MarketName
        {
            get { return _marketName; }
            set { _marketName = value; }
        }

        private byte _categoryID;
        [Required]
        [DataMember(IsRequired = true)]
        public byte CategoryID
        {
            get { return _categoryID; }
            set { _categoryID = value; }
        }

        private string _state;
        [Required]
        [MaxLength(50)]
        [DataMember(IsRequired = true)]
        public string State
        {
            get { return _state; }
            set { _state = value; }
        }

        private string _typeName;
        [MaxLength(50)]
        [DataMember()]
        public string TypeName
        {
            get { return _typeName; }
            set { _typeName = value; }
        }
        
        private DateTime? _updateTime;
        [DataMember]
        public DateTime? UpdateTime
        {
            get { return _updateTime; }
            set { _updateTime = value; }
        }

        private BFEvent _event;
        [IgnoreDataMember]
        public BFEvent Event
        {
            get { return _event; }
            set { _event = value; }
        }

        private ObservableCollection<BFRunner> _runners;
        [DataMember]
        public ObservableCollection<BFRunner> Runners
        {
            get { return _runners; }
            set
            {
                if (_runners == value) return;
                _runners = value;
                if (_runners != null)
                {
                    _runners.CollectionChanged += Runners_CollectionChanged;
                    foreach (var item in _runners)
                    {
                        item.Market = this;
                    }
                }
            }
        }

        private void Runners_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
            {
                foreach (BFRunner item in e.NewItems)
                {
                    if (item.Market != this)
                        item.Market = this;
                }
                if (e.OldItems != null)
                {
                    foreach (BFRunner item in e.OldItems)
                    {
                        item.Market = null;
                    }
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                if (e.OldItems != null)
                {
                    foreach (BFRunner item in e.OldItems)
                    {
                        item.Market = null;
                    }
                }
            }
        }
        
    }
}
